$(function() {
	var slider = 0;
	blogtype();

	//类别导航开关点击事件
	$('.category-toggle').click(function(e) {
		e.stopPropagation(); //阻止事件冒泡
		categroyIn();
	});
	//类别导航点击事件，用来关闭类别导航
	$('.article-category').click(function() {
		categoryOut();
	});
	//遮罩点击事件
	$('.blog-mask').click(function() {
		categoryOut();
	});
	$('.f-qq').on('click', function() {
		window.open('http://connect.qq.com/widget/shareqq/index.html?url=' + $(this).attr("href") +
			'&sharesource=qzone&title=' + $(this).attr("title") + '&pics=' + $(this).attr("cover") + '&summary=' + $(this).attr(
				"desc") + '&desc=你的分享简述' + $(this).attr("desc"));
	});
	$("body").delegate(".fa-times", "click", function() {
		$(".search-result").hide().empty();
		$("#searchtxt").val("");
		$(".search-icon i").removeClass("fa-times").addClass("fa-search");
	});
	$("#query-icon").click(function() {
		doRequest({
			"page": 1,
			"limit": 10,
			"queryParam": $('#searchtxt').val()
		});
	});

	//显示类别导航
	function categroyIn() {
		$('.category-toggle').addClass('layui-hide');
		$('.article-category').unbind('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend');
		$('.blog-mask').unbind('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend');
		$('.blog-mask').removeClass('maskOut').addClass('maskIn');
		$('.blog-mask').removeClass('layui-hide').addClass('layui-show');
		$('.article-category').removeClass('categoryOut').addClass('categoryIn');
		$('.article-category').addClass('layui-show');
	}
	//隐藏类别导航
	function categoryOut() {
		$('.blog-mask').on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
			$('.blog-mask').addClass('layui-hide');
		});
		$('.article-category').on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
			$('.article-category').removeClass('layui-show');
			$('.category-toggle').removeClass('layui-hide');
		});
		$('.blog-mask').removeClass('maskIn').addClass('maskOut').removeClass('layui-show');
		$('.article-category').removeClass('categoryIn').addClass('categoryOut');
	}

	function blogtype() {
		$('#category li').hover(function() {
			$(this).addClass('current');
			var num = $(this).attr('data-index');
			$('.slider').css({
				'top': ((parseInt(num) - 1) * 40) + 'px'
			});
		}, function() {
			$(this).removeClass('current');
			$('.slider').css({
				'top': slider
			});
		});
		$(window).scroll(function(event) {
			var winPos = $(window).scrollTop();
			if (winPos > 750)
				$('#categoryandsearch').addClass('fixed');
			else
				$('#categoryandsearch').removeClass('fixed');
		});
	};

	$('.doQuery').click(function() {
		let text = $(this).html();
		if (text == "全部文章") {
			$('#currType').html(0);
		}else if (text == "科文") {
			$('#currType').html(1);
		}else if (text == "乾文") {
			$('#currType').html(2);
		}else if (text == "源文") {
			$('#currType').html(3);
		}
		doRequest({
			"page": 1,
			"limit": 10
		});
		myRender({
			"page": 1,
			"limit": 10
		})
	});
	function doRequest(paramMap) {
		paramMap["type"] = $('#currType').html();
		paramMap["queryParam"] = $('#searchtxt').val();
		$.ajax({
			type: "get",
			url: urls.cliUrl + 'article/list/count',
			async: false,
			data: paramMap,
			success: function(res) {
				if (res.errorCode === 0) {
					$('#count').html(res.data);
				} else {
					console.log(res.msg);
				}
			}
		});
		$.ajax({
			type: "get",
			url: urls.cliUrl + 'article/list',
			async: true,
			data: paramMap,
			success: function(res) {
				if (res.code === 0) {
					$('#LAY_bloglist').html("");
					for (let art of res.data) {
						let typeName = "";
						if (art.type == 1) {
							typeName = "科文";
						} else if (art.type == 2) {
							typeName = "乾文";
						} else {
							typeName = "源文";
						}
						$('#LAY_bloglist').append(
							"<section class='article-item zoomIn article'><h5 class='title'><span class='fc-blue'>" + typeName +
							"</span><a href='read.html?id=" + art.id + "'>" + art.title +
							"</a></h5><div class='time'><span class='day'>" + timestampToDay(art.createTime) +
							"</span><span class='month fs-18'>" + timestampToMonth(art.createTime) +
							"<span class='fs-14'>月</span></span><span class='year fs-18 ml10'>" + timestampToYear(art.createTime) +
							"</span></div><div class='content'><a href='read.html?id=" + art.id +
							"' class='cover img-light'><img src='" + art.advertisingImage + "'></a>" + art.htmlContent +
							"</div><div class='read-more'><a href='read.html?id=" + art.id +
							"' class='fc-black f-fwb'>继续阅读</a></div><aside class='f-oh footer'><div class='f-fl tags'><span class='fa fa-tags fs-16'></span><a class='tag'>" +
							art.source +
							"</a></div><div class='f-fr'><span class='read'><i class='fa fa-eye fs-16'></i><i class='num'>" + art.pageView +
							"</i></span><span class='ml20'><i class='fa fa-comments fs-16'></i><a href='javascript:void(0)' class='num fc-grey'>1</a></span></div></aside></section>"
						);
					}
				} else {
					console.log(res.msg);
				}
			}
		});
	}
	/**
	 * 进入页面请求
	 */
	doRequest({
		"page": 1,
		"limit": 10
	});
	myRender({
		"page": 1,
		"limit": 10
	})
	/**
	 * 分页
	 * 
	 * @param {Object} paramMap
	 */
	function myRender(paramMap) {
		layui.use(['laypage'], function() {
			var laypage = layui.laypage;
			//分页
			laypage.render({
				elem: 'demo2-2',
				count: $('#count').html(),
				theme: '#FFB800',
				jump: function(obj, first) {
					if (!first) {
						let paramMap = {};
						paramMap["queryParam"] = $('#searchtxt').val();
						paramMap["page"] = obj.curr;
						paramMap["limit"] = obj.limit;
						doRequest(paramMap);
					}
					$('#demo2-2 .layui-laypage-prev').html("<<");
					$('#demo2-2 .layui-laypage-next').html(">>");
				}
			});
		});
	}
	$.ajax({
		type: "get",
		url: urls.cliUrl + 'article/list/hot',
		async: true,
		data: {
			"pageNo": 1,
			"pageSize": 8
		},
		success: function(res) {
			if (res.errorCode === 0) {
				$('#hotArticle').html("");
				for(let hot of res.data){
					$('#hotArticle').append("<li> <a href='read.html?id=" + hot.id + "'>" + hot.title + "</a></li>");
				}
			} else {
				console.log(res.description);
			}
		}
	});
	$.ajax({
		type: "get",
		url: urls.cliUrl + 'article/list/hot',
		async: true,
		data: {
			"pageNo": 1,
			"pageSize": 3
		},
		success: function(res) {
			if (res.errorCode === 0) {
				$('#topArticle').html("");
				for(let hot of res.data){
					$('#topArticle').append("<li> <a href='read.html?id=" + hot.id + "'>" + hot.title + "</a></li>");
				}
			} else {
				console.log(res.description);
			}
		}
	});
});
